import processing.core.*;

public class Pullo extends Esine {

	float bottomRadius;
	float tall;
//	int sides;
	float rotx = PApplet.PI/4;
	float roty = PApplet.PI/4;
	PImage img;
	PImage img2;

	Pullo(PApplet parent, int pts){
		super(parent, pts);
		img = pa.loadImage("textures/Etiketti.jpg");
		img2 = pa.loadImage("textures/musta.png");
	}
	
	public void piirra(){
		super.piirra(); // rotate and transform
		pa.translate(0,110,0);
		pa.scale((float) 0.7);
		//Piirrettn pullon etikettiosa
		pohja(30,120,64,img);
		pa.pushMatrix();
		pa.translate(0, 40, 0);
		keskikuori(img);
		pa.popMatrix();

		//piirretn kaula
		pa.pushMatrix();
		pa.translate(0, -40, 0);
		kuori(30, 10, -30, 64, img2);  
		pa.popMatrix();

		//piirretn korkki
		pa.pushMatrix();
		pa.translate(0,-90,0); 
//		pa.fill(0);
		kuori(10,10,20,64, img2);
		pa.popMatrix();

		//piirretn korkin pllys
		pa.pushMatrix();
		pa.translate(0,-110,0); 
//		pa.fill(0);
		pohja(10,20,64, img2);
		pa.popMatrix();
		super.poppi(); // revert rotate and transform
	}

	void pohja(float bottomRadius, float tall, int sides, PImage img2){
		float angle = 0;
		float angleIncrement = PConstants.TWO_PI / sides;   

		if (bottomRadius != 0) {
			angle = 0;
			pa.beginShape(PConstants.TRIANGLE_FAN);
			pa.texture(img2);


			// Keskipiste
			pa.vertex(0, tall, 0);
			for (int i = 0; i < sides + 1; i++) {
				pa.vertex(bottomRadius * PApplet.cos(angle), tall, bottomRadius * PApplet.sin(angle));
				angle += angleIncrement;
			}
			pa.endShape();
		}

	}

	void kuori(float topRadius, float bottomRadius, float tall, int sides, PImage img2){

		float angle = 0;
		float angleIncrement = PConstants.TWO_PI / sides;
		pa.beginShape(PConstants.QUAD_STRIP);
		pa.texture(img2);

		for (int i = 0; i < sides + 1; ++i) {
			pa.vertex(topRadius*PApplet.cos(angle), 0, topRadius*PApplet.sin(angle));
			pa.vertex(bottomRadius*PApplet.cos(angle), tall, bottomRadius*PApplet.sin(angle));
			angle += angleIncrement;
		}
		pa.endShape();
	}

	void keskikuori(PImage img){
		int tubeRes = 70;
		float[] tubeX = new float[tubeRes];
		float[] tubeY = new float[tubeRes];


		float angle = (float)(370.0 / tubeRes);
		for (int i = 0; i < tubeRes; i++) {
			tubeX[i] = PApplet.cos(PApplet.radians(i * angle));
			tubeY[i] = PApplet.sin(PApplet.radians(i * angle));
		}


		pa.beginShape(PConstants.QUAD_STRIP);
		pa.texture(img);
		for (int i = 0; i < tubeRes; i++) {
			float x = tubeX[i] * 30;
			float z = tubeY[i] * 30;
			float u = img.width / tubeRes * i;
			pa.vertex(x, -80, z, u, 0);
			pa.vertex(x, 80, z, u, img.height);
		}
//		pa.fill(0);
		pa.endShape();
	}
}